package com.hutao.medical.controller.statics;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.util.CookieUtils;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.pojo.audit.AuditRecord;
import com.hutao.medical.pojo.audit.AuditRecordDetail;
import com.hutao.medical.service.AuditRecordDetailService;
import com.hutao.medical.service.FirstLevelMedicalService;

@RequestMapping("/medRecordAuditDetail")
@Controller
public class MedRecordAuditDetailController {
	Logger log = LoggerFactory.getLogger(MedRecordAuditDetailController.class);
	
	@Autowired
	private AuditRecordDetailService auditRecordDetailService;
	
	@Autowired
	private FirstLevelMedicalService firstLevelMedicalService;
	
	@ResponseBody
	@PostMapping("/addAuditRecordDetail")
	public Result addAuditRecordDetail(@RequestBody AuditRecord auditRecord, 
			HttpServletRequest request, HttpSession session) {
		try {
			String operationName = CookieUtils.getCookieValue(request, "userName");;
			Map<String, Object> firstLevelMedical = firstLevelMedicalService.findFirstLevelMedicalByFirstGuid(auditRecord.getFirstGuid());
			if(null == firstLevelMedical) {
				return new Result(false, "没有该病案");
			}
			
			Result result = auditRecordDetailService.insertAuditRecordDetail(auditRecord.getFirstGuid(), operationName, auditRecord, firstLevelMedical);
			return result;
		} catch (Exception e) {
			System.out.println(e);
			log.error(e.getMessage());
			return new Result(false, "系统异常!");
		}
		
	}
	
	@ResponseBody
	@GetMapping("/findAuditRecordDetailList")
	public Map<String, Object> findAuditRecordDetailList(@RequestParam(value = "firstGuid", required = false) String firstGuid) {
		Map<String, Object> resultValueMap = new HashMap<>();
		List<AuditRecordDetail> auditRecordDetailList = auditRecordDetailService.findAuditRecordDetailListByFirstGuid(firstGuid);
		resultValueMap.put("auditRecordDetailList", auditRecordDetailList);
		return resultValueMap;
	}
	
	@ResponseBody
	@PostMapping("/updateAuditRecordDetailList")
	public Result updateAuditRecordDetailList(AuditRecordDetail auditRecordDetail) {
		try {
			if(auditRecordDetailService.updateAuditRecordDetail(auditRecordDetail) > 0) {
				return new Result(true, "修改成功!");
			}
			return new Result(false, "添加失败!");
		} catch (Exception e) {
			System.out.println(e);
			log.error(e.getMessage());
			return new Result(false, "系统异常!");
		}
		
	}
	
	@ResponseBody
	@GetMapping("/findAuditRecordDetailListById")
	public Map<String, Object> findAuditRecordDetailListById(Integer id) {
		Map<String, Object> resultValueMap = new HashMap<>();
		AuditRecordDetail auditRecordDetail = auditRecordDetailService.findAuditRecordDetailListById(id);
		resultValueMap.put("auditRecordDetail", auditRecordDetail);
		return resultValueMap;
	}
	
	@ResponseBody
	@PostMapping("/updateStatusById")
	public Result updateStatusById(Integer id) {
		try {
			if(auditRecordDetailService.updateStatusById(id) > 0) {
				return new Result(true, "修改成功!");
			}
			return new Result(false, "系统繁忙,请重新再刷新!");
		} catch (Exception e) {
			System.out.println(e);
			log.error(e.getMessage());
			return new Result(false, "系统异常!");
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	

}
